<template>
	<view class="container">
		<!-- 顶栏 -->
		<view class="top-bar fixed">
			<view class="logo-search-container">
				<image class="logo" src="/static/logo.png" mode="widthFix" />
				<view class="search-bar">
					<input type="text" placeholder="搜索" @input="onSearchInput" />
					<i class="ri-search-line search-icon"></i>
				</view>
			</view>
		</view>

		<!-- 社区动态 -->
		<view v-for="(community, index) in communityList" :key="community.communityId" class="community-card" @click="goToDetail(community.communityId)">
			<view class="card-header">
				<image :src="community.candidate.candidateImg" class="avatar" />
				<view class="header-info">
					<text class="candidate-name">{{ community.candidate.name }}</text>
					<text class="time">{{ formatTime(community.time) }}</text>
				</view>
			</view>
			<view class="card-content">
				<text class="community-title">{{ community.communityTitle }}</text>
				<view v-if="community.communityType === 'video'">
					<text>{{ community.communityContent.substring(0, 30) }}...</text>
					<br />
					<video @click.stop :src="community.videoPath" controls />
				</view>
				<view v-if="community.communityType === 'image'">
					<text>{{ community.communityContent.substring(0, 30) }}...</text>
					<view class="image-grid" @click.stop>
						<image v-for="(img, imgIndex) in community.imageList" :key="imgIndex" :src="img" mode="widthFix" @click="onImageClick(community.imageList, imgIndex)" />
					</view>
				</view>
				<view v-if="community.communityType === 'article'">
					<text>{{ community.communityContent.substring(0, 80) }}...</text>
				</view>
			</view>
			<view class="card-footer" @click.stop>
				<view class="icon-group">
					<view @click="onCommentClick(community)" class="icon-item">
						<i :class="community.isCommented ? 'ri-chat-3-fill highlight' : 'ri-chat-3-line'" class="icon"></i>
						<text>{{ community.commentCount }}</text>
					</view>
					<view @click="onLikeClick(community)" class="icon-item">
						<i :class="community.isLiked ? 'ri-thumb-up-fill highlight' : 'ri-thumb-up-line'" class="icon"></i>
						<text>{{ community.likeCount }}</text>
					</view>
					<view @click="onStarClick(community)" class="icon-item">
						<i :class="community.isStarred ? 'ri-star-fill highlight' : 'ri-star-line'" class="icon"></i>
						<text>{{ community.starCount }}</text>
					</view>
				</view>
				<text v-if="community.reportStatus > 20" class="report-warning">该动态被多人举报，请注意真实性</text>
			</view>
		</view>

		<!-- 悬浮添加按钮 -->
		<button class="float-button" @click="onAddCommunityClick">
			<i class="ri-add-fill add-icon"></i>
		</button>

		<!-- 全屏遮罩弹窗选择动态类型 -->
		<view v-if="showModal" :class="['modal', modalClosing ? 'modal-closing' : '']" @click="onModalClick">
			<view :class="['modal-content', modalClosing ? 'modal-content-closing' : '']" @click.stop>
				<view class="modal-header">
					<text class="modal-title">HI，你要发送什么动态？</text>
					<i class="ri-close-line close-icon" @click="onModalClick"></i>
				</view>
				<view class="option" @click="onSelectType('article')">
					<i class="ri-file-text-line option-icon"></i>
					<text class="option-text">文章</text>
				</view>
				<view class="option" @click="onSelectType('image')">
					<i class="ri-image-line option-icon"></i>
					<text class="option-text">图文</text>
				</view>
				<view class="option" @click="onSelectType('video')">
					<i class="ri-video-line option-icon"></i>
					<text class="option-text">视频</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			communityList: [],
			showModal: false,
			modalClosing: false,
			current: 1,
			size: 5,
			total: 0,
			debounceTimer: null,
			debounceTimer2: null
		};
	},
	methods: {
		// 搜索动态
		onSearchInput(event) {
			// 处理搜索逻辑
		},
		// 评论点击逻辑
		onCommentClick(community) {
			if (community.isCommented) {
				uni.showToast({
					title: '不能重复评论',
					icon: 'error'
				});
				return;
			}
			uni.navigateTo({
				url: '/pages/community/detail?id=' + community.communityId + '&isComment=true'
			});
		},
		// 点赞点击逻辑
		onLikeClick(community) {
			// 如果是用户第一次点击，记录原始点赞状态
			if (community.originalLiked === undefined) {
				community.originalLiked = community.isLiked;
			}
			// 更新点赞状态
			community.isLiked = !community.isLiked;
			community.isLiked ? community.likeCount++ : community.likeCount--;
			// 防抖逻辑：2秒后发送请求
			clearTimeout(this.debounceTimer);
			this.debounceTimer = setTimeout(() => {
				// 如果状态和首次状态不一致，发送请求
				if (community.isLiked !== community.originalLiked) {
					if (community.isLiked) {
						this.$api
							.likeCommunity({
								communityId: community.communityId,
								candidateId: uni.getStorageSync('user').candidateId
							})
							.then(() => {
								uni.showToast({
									title: '点赞成功',
									icon: 'none'
								});
								community.originalLiked = undefined;
							});
					} else {
						this.$api
							.unlikeCommunity({
								communityId: community.communityId,
								candidateId: uni.getStorageSync('user').candidateId
							})
							.then(() => {
								uni.showToast({
									title: '取消点赞成功',
									icon: 'none'
								});
								community.originalLiked = undefined;
							});
					}
				} else {
					console.log('点赞状态未改变');
				}
			}, 2000);
		},
		// 收藏点击逻辑
		onStarClick(community) {
			if (community.originalStared === undefined) {
				community.originalStared = community.isStarred;
			}
			community.isStarred = !community.isStarred;
			community.isStarred ? community.starCount++ : community.starCount--;
			clearTimeout(this.debounceTimer2);
			this.debounceTimer2 = setTimeout(() => {
				if (community.isStarred !== community.originalStared) {
					if (community.isStarred) {
						this.$api
							.addCollection({
								communityId: community.communityId,
								candidateId: uni.getStorageSync('user').candidateId
							})
							.then(() => {
								uni.showToast({
									title: '收藏成功',
									icon: 'none'
								});
							});
						community.originalStared = undefined;
					} else {
						this.$api
							.deleteCollection({
								communityId: community.communityId,
								candidateId: uni.getStorageSync('user').candidateId
							})
							.then(() => {
								uni.showToast({
									title: '取消收藏成功',
									icon: 'none'
								});
							});
						community.originalStared = undefined;
					}
				} else {
					console.log('收藏状态未改变');
				}
			}, 2000);
		},
		// 格式化时间
		formatTime(time) {
			const diff = Date.now() - new Date(time);
			if (diff < 60 * 1000) {
				return `${Math.floor(diff / 1000)}秒前`;
			} else if (diff < 60 * 60 * 1000) {
				return `${Math.floor(diff / (60 * 1000))}分钟前`;
			} else if (diff < 24 * 60 * 60 * 1000) {
				return `${Math.floor(diff / (60 * 60 * 1000))}小时前`;
			} else {
				return `${Math.floor(diff / (24 * 60 * 60 * 1000))}天前`;
			}
		},
		// 添加动态点击逻辑
		onAddCommunityClick() {
			this.showModal = true;
		},
		// 关闭动态模态框
		onModalClick() {
			this.modalClosing = true;
			setTimeout(() => {
				this.showModal = false;
				this.modalClosing = false;
			}, 300);
		},
		// 发布动态
		onSelectType(type) {
			console.log('Selected type:', type);
			uni.navigateTo({
				url: '/pages/community/publish?dynamicType=' + type
			});
		},
		// 预览大图
		onImageClick(imgPath, index) {
			uni.previewImage({
				urls: imgPath,
				current: index
			});
		},
		// 前往动态详情页
		goToDetail(id) {
			uni.navigateTo({
				url: '/pages/community/detail?id=' + id
			});
		}
	},
	mounted() {
		this.$api
			.getCommunity({
				current: this.current,
				size: this.size,
				candidateId: uni.getStorageSync('user').candidateId
			})
			.then((res) => {
				this.communityList = res.records;
				this.total = res.total;
			});
	}
};
</script>

<style scoped>
@import '@/static/remixicon.css';

.container {
	padding: 10px;
	padding-top: 70px;
}
.top-bar {
	background-color: #59a0ff;
	padding: 10px;
	position: fixed;
	top: 0;
	left: 0;
	right: 0;
	z-index: 100;
}
.logo-search-container {
	display: flex;
	align-items: center;
}
.logo {
	width: 40px;
	height: 40px;
	margin-right: 10px;
}
.search-bar {
	flex: 1;
	position: relative;
	border-radius: 20px;
	background-color: white;
	padding: 5px 10px;
	display: flex;
	align-items: center;
}
.search-bar input {
	flex: 1;
	border: none;
	outline: none;
	padding: 5px;
	font-size: 14px;
}
.search-icon {
	font-size: 18px;
	color: #59a0ff;
}
.community-title {
	font-size: 18px;
	font-weight: bold;
	margin-bottom: 10px;
}
.community-card {
	margin-bottom: 20px;
	padding: 15px;
	border: 1px solid #e0e0e0;
	border-radius: 10px;
	box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
	background-color: #ffffff;
}
.card-header {
	display: flex;
	align-items: center;
}
.header-info {
	margin-left: 10px;
}
.avatar {
	width: 50px;
	height: 50px;
	border-radius: 25px;
}
.candidate-name {
	font-weight: bold;
	font-size: 16px;
}
.time {
	font-size: 12px;
	color: #999;
	margin-left: 10px;
}
.card-content {
	margin-top: 10px;
}
.image-grid {
	display: flex;
	flex-wrap: wrap;
	gap: 5px;
	margin-top: 10px;
}
.image-grid image {
	width: 30%;
	border-radius: 5px;
}
.card-footer {
	display: flex;
	justify-content: space-between;
	margin-top: 15px;
	align-items: center;
}
.icon-group {
	display: flex;
	justify-content: space-between;
	width: 100%;
}
.icon-item {
	display: flex;
	align-items: center;
	cursor: pointer;
	flex: 1;
	justify-content: center;
}
.icon {
	font-size: 24px;
	margin-right: 5px;
}
.highlight {
	color: #59a0ff;
}
.report-warning {
	color: red;
	font-size: 12px;
	text-align: right;
}
.float-button {
	position: fixed;
	bottom: 70px;
	right: 20px;
	background-color: #59a0ff;
	color: white;
	border-radius: 50%;
	width: 50px;
	height: 50px;
	display: flex;
	align-items: center;
	justify-content: center;
	z-index: 101;
}
.add-icon {
	font-size: 30px;
	color: white;
}
.modal {
	position: fixed;
	top: 0;
	bottom: 0;
	left: 0;
	right: 0;
	background-color: rgba(0, 0, 0, 0.5);
	display: flex;
	align-items: flex-end;
	z-index: 102;
	animation: fadeIn 0.3s ease forwards; /* 淡入动画 */
}

.modal-closing {
	animation: fadeOut 0.3s ease forwards; /* 淡出动画 */
}

.modal-content {
	background-color: #ffffff;
	width: 100%;
	border-top-left-radius: 20px;
	border-top-right-radius: 20px;
	padding: 20px;
	animation: slideUp 0.3s ease forwards; /* 滑入动画 */
}

.modal-content-closing {
	animation: slideDown 0.3s ease forwards; /* 滑出动画 */
}
.modal-header {
	display: flex;
	justify-content: space-between;
	align-items: center;
	margin-bottom: 20px;
}
.modal-title {
	font-size: 18px;
	font-weight: bold;
}
.close-icon {
	font-size: 24px;
	color: #333;
	cursor: pointer;
}
.option {
	display: flex;
	align-items: center;
	padding: 15px;
	border-radius: 10px;
	margin-bottom: 10px;
	background-color: #f5f5f5;
	cursor: pointer;
}
.option-icon {
	font-size: 24px;
	margin-right: 10px;
	color: #007aff;
}
.option-text {
	font-size: 16px;
	font-weight: bold;
}
@keyframes fadeIn {
	from {
		opacity: 0;
	}
	to {
		opacity: 1;
	}
}

@keyframes fadeOut {
	from {
		opacity: 1;
	}
	to {
		opacity: 0;
	}
}

@keyframes slideUp {
	from {
		transform: translateY(100%);
	}
	to {
		transform: translateY(0);
	}
}

@keyframes slideDown {
	from {
		transform: translateY(0);
	}
	to {
		transform: translateY(100%);
	}
}
</style>
